<?php

namespace App\Http\Controllers;

use App\Http\Requests\OrderRequest;
use App\Models\ProductSku;
use App\Models\UserAddress;
use App\Models\Order;
use Carbon\Carbon;
use Illuminate\Http\Request;



class OrdersController extends Controller
{
    public function store(OrderRequest $request)
    {
        $user  = $request->user();
        // 开启一个数据库事务
        $order = \DB::transaction(function () use ($user, $request) {
            // 更新此地址的最后使用时间
            // 创建一个订单
            $order = new Order([
                'address' => [ // 将地址信息放入订单中
                    'address' => '',
                    'zip' => '',
                    'contact_name' => '',
                    'contact_phone' => '',
                ],
                'remark' => $request->input('remark'),
                'total_amount'  => 0,
                'refund_status' => '',
                'ship_status'   => '',
            ]);
            // 订单关联到当前用户
            $order->user()->associate($user);
            // 写入数据库
            $order->save();

            $totalAmount = 0;
            $sku_id = $request->input('sku_id');
            $amount = $request->input('amount');
            // 遍历用户提交的 SKU

//            foreach ([$items] as $data) {
                $sku  = ProductSku::find($sku_id);
                // 创建一个 OrderItem 并直接与当前订单关联
                $item = $order->items()->make([
                    'amount' => $amount,
                    'price' => $sku->price,
                ]);
                $item->product()->associate($sku->product_id);
                $item->productSku()->associate($sku);
                $item->save();
                $totalAmount += $sku->price * $amount;
//            }

            // 更新订单总金额
            $order->update(['total_amount' => $totalAmount]);

            // 将下单的商品从购物车中移除
//            $skuIds = collect($items)->pluck('sku_id');
//            $user->cartItems()->whereIn('product_sku_id', $skuIds)->delete();

            return $order;
        });

        return $order;
    }


    public function edit($no, Order $order)
    {
        $order = $order->with('items', 'items.product', 'items.productSku')
            ->where('no', $no)->first();

        $this->authorize('update', $order);

        return view('product.order', compact('order'));
    }


    /***
     * 绑定交易帐号
     * @param $no
     * @param OrderRequest $request
     * @param Order $order
     */
    public function update($no, OrderRequest $request, Order $order)
    {
        $order = $order->where('no', $no)->first();
        $this->authorize('update', $order);
        if (! $order->trade_account){
            $order->trade_account = $request->trade_account;
            $res = $order->save();
            if ($res){
                return redirect()->route('order.payment', $order->no);
            }
        }

        return back()->withInput();
    }


    public function payment($no, Order $order)
    {
        $order = $order->with('items', 'items.product', 'items.productSku')
            ->where('no', $no)->first();

        $this->authorize('update', $order);

        return view('product.payment', compact('order'));
    }


    public function determine(Request $request, Order $order)
    {
        $order = $order->where('no', $request->no)->first();
        $this->authorize('update', $order);
        $order->paid_at = date('Y-m-d H:i:s');
        if ($order->save()){
            session()->flash('success', '提交成功~');
            return back();
        }else{
            session()->flash('danger', '订单异常~');
            return back();
        }
    }





}
